#include "mainScreenLogic.h"
#include "./../gameManager.h"
#include "MainScreenPictures.h"
#include "CommonDefinition.h"

MainScreenLogic::MainScreenLogic()
	: m_pPicturesLayer(NULL)
	, m_pTouchLayer(NULL)
	, m_eCurRound(e_enemyRound)
	, m_pRoundDoneListener(NULL)
{

}

MainScreenLogic::~MainScreenLogic()
{

}

bool MainScreenLogic::init()
{
	if (cocos2d::Layer::init())
	{
		m_pTouchLayer = MainScreenTouchLayer::create();
		m_pTouchLayer->setTouchListener(this);
		addChild(m_pTouchLayer);

		m_pPicturesLayer = MainScreenPictures::create();
		m_pPicturesLayer->setroundDoneListener(this);
		addChild(m_pPicturesLayer);

		initCollision();
		return true;
	}

	return false;
}

void MainScreenLogic::onTouchesBegan(const std::vector<moveVec2> &vecVec2s)
{
	if (m_pPicturesLayer)
	{
		m_pPicturesLayer->drawControlLine(vecVec2s[0].startPos, vecVec2s[0].moveVec);
	}
}
void MainScreenLogic::onTouchesMoved(const std::vector<moveVec2> &vecVec2s)
{
	if (m_pPicturesLayer)
	{
		m_pPicturesLayer->drawControlLine(vecVec2s[0].startPos, vecVec2s[0].moveVec);
		m_pPicturesLayer->driveSwords(vecVec2s[0].moveVec);
	}
}
void MainScreenLogic::onTouchesEnded(const std::vector<moveVec2> &vecVec2s)
{
	if (m_pPicturesLayer)
	{
		m_pPicturesLayer->hideControlLine();
	}
}

void MainScreenLogic::roundDone(doneType done)
{
	if (roundDoneListener::e_heroDone == done)
	{
		//turnRound(e_enemyRound);
		turnRound(e_heroRound);
	}
	else
	if (roundDoneListener::e_enemyDone == done)
	{
		//turnRound(e_heroRound);
		turnRound(e_enemyRound);
	}
}

void MainScreenLogic::turnRound(curRound roundType)
{
	if (e_heroRound == roundType)
	{
//		if (e_heroRound != m_eCurRound)
		{
			if (m_pPicturesLayer)
			{
				m_pPicturesLayer->heroRoundBegan();
				m_eCurRound = e_enemyRound;
			}
		}
//		else
//		{
//			cocos2d::log("error! curRound hero!");
//		}
	}
	else
	if (e_enemyRound == roundType)
	{
//		if (e_enemyRound != m_eCurRound)
		{
			if (m_pPicturesLayer)
			{
				m_pPicturesLayer->enemyRoundBegan();
				m_eCurRound = e_heroRound;
			}
		}
//		else
//		{
//			cocos2d::log("error! curRound enemy!");
//		}
	}
}

void MainScreenLogic::initCollision()
{
	auto listener = cocos2d::EventListenerPhysicsContact::create();
	listener->onContactBegin = [&](cocos2d::PhysicsContact& contact)
	{
		auto body1 = contact.getShapeA()->getBody();
		auto body2 = contact.getShapeB()->getBody();

		if (body1 && body1->getTag() == SWORD_PHYBODY_TAG && body2 && body2->getTag() == ENEMY_PHYBODY_TAG)
		{
			m_pPicturesLayer->attackCollisionBegan(body1, body2);
		}
		else if (body1 && body1->getTag() == ENEMY_PHYBODY_TAG && body2 && body2->getTag() == SWORD_PHYBODY_TAG)
		{
			m_pPicturesLayer->attackCollisionBegan(body2, body1);
		}
		else if (body1 && body1->getTag() == WORLD_BOUNDARY_TAG && body2)
		{
			m_pPicturesLayer->boundaryCollisionBegan(body2);
		}
		else if (body2 && body2->getTag() == WORLD_BOUNDARY_TAG && body1)
		{
			m_pPicturesLayer->boundaryCollisionBegan(body1);
		}
		else if (body1 && body1->getTag() == OBSTACLES_PHYBODY_TAG && body2)
		{
			m_pPicturesLayer->boundaryCollisionBegan(body2);
		}
		else if (body2 && body2->getTag() == OBSTACLES_PHYBODY_TAG && body1)
		{
			m_pPicturesLayer->boundaryCollisionBegan(body1);
		}
		else if (body1 && body1->getTag() == SWORD_PHYBODY_TAG && body2 && body2->getTag() == RANGEDWEAPONS_PHYBODY_TAG){
			m_pPicturesLayer->dealRWCollisionWithSword(body2, body1);
		}
		else if (body1 && body1->getTag() == RANGEDWEAPONS_PHYBODY_TAG && body2 && body2->getTag() == SWORD_PHYBODY_TAG){
			m_pPicturesLayer->dealRWCollisionWithSword(body1, body2);
		}
		else if (body1 && body1->getTag() == RANGEDWEAPONS_PHYBODY_TAG && body2 && body2->getTag() == HERO_PHYBODY_TAG){
			m_pPicturesLayer->dealRWCollisionWithHero(body1);
		}
		else if (body1 && body1->getTag() == HERO_PHYBODY_TAG && body2 && body2->getTag() == RANGEDWEAPONS_PHYBODY_TAG){
			m_pPicturesLayer->dealRWCollisionWithHero(body2);
		}

		return true;
	};


	listener->onContactPreSolve = [](cocos2d::PhysicsContact& contact,
		cocos2d::PhysicsContactPreSolve& solve)
	{
		return true;
	};


	listener->onContactPostSolve = [](cocos2d::PhysicsContact& contact,
		const cocos2d::PhysicsContactPostSolve& solve)
	{
	};


	listener->onContactSeparate = [](cocos2d::PhysicsContact& contact)
	{
	};

	cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1);
}